<?php
namespace Xuan\Controller;
class GiftsController extends CommonController {

    public function lists() {
        $sql = "is_mall=1";
        $keywords = trim(I('get.keywords'));
        $pid = I('get.pid', '', 'int');
        if ($keywords) {
            $sql .= " AND title like '%" . $keywords . "%'";
        }
        if ($pid) {
            $sql .= " AND pid = '" . $pid . "'";
        }
        $count = M('gifts')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M('gifts')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('ord Asc,id desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->assign("pid", $pid);
        $this->display();
    }

    public function detail() {
        $id = I('get.id', '', 'int');
        $cats = getDictionary('gifts_cat', 0);
        if ($id > 0) {
            $detail = M("gifts")->where("id = " . $id . "")->find();
            if ($detail['is_start'] == 0) {
                $detail['starttime'] = date("Y-m-d");
                $detail['endtime'] = date("Y-m-d", strtotime("+1 day"));
            } else {
                $detail['starttime'] = date("Y-m-d", $detail['starttime']);
                $detail['endtime'] = date("Y-m-d", $detail['endtime']);
            }
        } else {
            $detail['starttime'] = date("Y-m-d");
            $detail['endtime'] = date("Y-m-d", strtotime("+1 day"));
            $detail['pid'] = $cats[0]['id'];
        }
        $cat = getStaticInfo($detail['pid'], "dictionary");
        
        $this->assign("cat", $cat);
        $this->assign("cats", $cats);
        $this->assign("detail", $detail);
        $this->display();
    }

    public function detail_post() {
        $id = I('post.id', '', 'int');
        $path = C('picFile');
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->rootPath = $path; // 设置附件上传目录
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        if ($info) {
            $uploadInfo = $info['pic'];
            $data['pic'] = $uploadInfo['savename']; //数据库保存名
        }
        $data['title'] = I('post.title');
        $data['ord'] = I('post.ord', '30', 'int');
        $data['is_mall'] = 1;
        $data['is_award'] = 0;
        $data['points'] = I('post.points', '0', 'int');
            $data['pid'] = I('post.pid', '', 'int');
        $data['bigtitle'] = '';
        $data['pointsAward'] = 0;
            $data['percent'] = 0;
            $data['rank'] = 0;

        if ($id > 0) {
            M("gifts")->where("id = " . $id . "")->save($data);
            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            M("gifts")->add($data);
            $this->success('添加成功！', U('Gifts/lists'));
        }
        clearTempFile();
    }
    public function cat() {//职称级别
        $lists = getDictionary('gifts_cat', 0);
        $this->assign("lists", $lists);
        $this->display();
    }
    public function cat_detail() {//礼品分类详情
        $id = I("get.id", 0, 'int');
        $detail = getStaticInfo($id, "dictionary");
        $this->assign("detail", $detail);
        $this->display();
    }

    public function cat_detail_post() {
        $id = I("post.id", 0, 'int');
        $data['name'] = trim(I("post.name"));
        $data['ord'] = I("post.ord", 0, 'int');
        if ($id > 0) {
            M("dictionary")->where("id = " . $id . "")->save($data);
            $this->success("修改礼品分类成功！", session('QUERY_STRING'));
        } else {
            $data['code'] = 'gifts_cat';
            M("dictionary")->add($data);
            $this->success("添加礼品分类成功！", U("Gifts/cat"));
        }
        clearTempFile();
    }

    
    public function exchange() {
         $sql = "1=1";
         $keywords = trim(I('get.keywords'));
        $pid = I('get.pid', '', 'int');
        if ($keywords) {
            $sql .= " AND truename like '%" . $keywords . "%'";
        }
        if ($pid) {
            $sql .= " AND pid = '" . $pid . "'";
        }
        $count = M('gifts_exchange')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M('gifts_exchange')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->assign("pid", $pid);
        $this->display();
    }
    public function draw() {
        $sql = "is_award = 1";
        $count = M('gifts')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('gifts')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('ord Asc,id desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->display();
    }
    public function draw_detail() {
        $id = I('get.id', '', 'int');
        $cats = getDictionary('gifts_cat', 0);
        if ($id > 0) {
            $detail = M("gifts")->where("id = " . $id . "")->find();
        } else {
            $detail['pointsAward'] = 0;
        }
        $cat = getStaticInfo($detail['pid'], "dictionary");
        $this->assign("cat", $cat);
        $this->assign("cats", $cats);
        $this->assign("detail", $detail);
        $this->display();
    }

    public function draw_detail_post() {
        $id = I('post.id', '', 'int');
        $path = C('picFile');
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->rootPath = $path; // 设置附件上传目录
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        if ($info) {
            $uploadInfo = $info['pic'];
            $data['pic'] = $uploadInfo['savename']; //数据库保存名
        }
         $data['title'] = I('post.title');
        $data['ord'] = I('post.ord', '30', 'int');
        $data['is_mall'] = 0;
        $data['is_award'] = 1;
        $data['points'] = 0;
        $data['pid'] = 0;
        $data['bigtitle'] = I("post.bigtitle");
        $data['pointsAward'] = I('post.pointsAward', '0', 'int');
        $data['percent'] = I('post.percent');
        $data['rank'] = I("post.rank");
        if($data['rank']>0){
            $data['pointsAward'] = 0;
        }else{
            $data['pointsAward'] = I('post.pointsAward', '0', 'int');
        }
        if ($id > 0) {
            M("gifts")->where("id = " . $id . "")->save($data);
            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            M("gifts")->add($data);
            $this->success('添加成功！', U('Draw/lists'));
        }
        clearTempFile();
    }
    public function award() {
        $sql = "1=1";
        $keywords = trim(I('get.keywords'));
        $pid = I('get.pid', '', 'int');
        if ($keywords) {
            $sql .= " AND gift_name like '%" . $keywords . "%'";
        }
        $count = M('gifts_award')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('gifts_award')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->assign("pid", $pid);
        $this->display();
    }

}

?>
